package online

import (
	"server/common"

	"github.com/mitchellh/mapstructure"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *OnlineService) handleStopPublishAck(msg common.Message, from common.ServiceInfo,
	connId string) error {
	common.Logger(common.SERVICE_TYPE_ONLINE).WithField("msg", common.ToJson(msg)).
		Info("handling stop publish ack")

	if msg.Data == nil {
		LOG(msg.Rid).Error("stop publish ack data is nil")
		return nil
	}

	var msgData common.ResMsgData
	if err := mapstructure.Decode(msg.Data, &msgData); err != nil {
		LOG(msg.Rid).WithError(err).Error("failed to decode stop publish ack data")
		return nil
	}

	if msgData.Code != 0 {
		LOG(msg.Rid).Errorf("stop publish failed: %s", msgData.Msg)
		return nil
	}

	var ackData common.P2PStopPublishAckData
	if err := mapstructure.Decode(msgData.Data, &ackData); err != nil {
		LOG(msg.Rid).WithError(err).Error("failed to decode stop publish ack data")
		return nil
	}

	common.Logger(common.SERVICE_TYPE_ONLINE).Info("stop publish acknowledged successfully")

	return nil
}
